// var compareVersion = function(version1, version2) {
//   const arr1 = version1.split('.')      // ["1", "0", "0"]
//   const arr2 = version2.split('.')    // split version1 and version2 into arrays
//   console.log(arr1, arr2)
//   while(arr1.length && arr2.length){  // while arr1 and arr2 have elements
//     const n1 = Number(arr1.shift())  // shift the first element of arr1 and convert it to a number
//     const n2 = Number(arr2.shift()) // shift the first element of arr2 and convert it to a number
//     if(n1 > n2) return 1
//     if(n1 < n2) return -1
//     //return 0
//   }
//   console.log(arr1, arr2)
//   // if arr1 is empty, then arr2 is longer
//   if(arr1.length){
//     // arr2 数组已经为空
//     return arr1.every(item=>Number(item)===0) ? 0 : 1 // 如果arr1中的元素都为0，则返回0，否则返回1
//   }
//   if(arr2.length){
//     // arr1 数组已经为空
//     return arr2.every(item=>Number(item)===0) ? 0 : -1  // 如果arr2中的元素都为0，则返回0，否则返回-1
//   }
//   return 0;
// };
// let version1 = '1.0.01' // 1.0.0
// let version2 = '1.1.0002';
// console.log(compareVersion(version1, version2));


var compareVersion = function(version1, version2) {
  let arr1 = version1.split('.')      // ["1", "0", "0"]
  let arr2 = version2.split('.')    // split version1 and version2 into arrays
  console.log(arr1, arr2)
  let n =Math.max(arr1.length, arr2.length)
  if(arr1.length !==n){
    for (let i = 0; i < n - arr1.length+1; i++) {
      arr1.push('0')
  }
  }
  if (arr2.length !==n) {
    for (let i = 0; i < n - arr2.length+1; i++) {
      arr2.push('0')
  }
  }
  console.log(arr1, arr2)
  let i = 0
  while(i < n){
    const n1 = parseInt(arr1[i])  // shift the first element of arr1 and convert it to a number
    const n2 = parseInt(arr2[i]) // shift the first element of arr2 and convert it to a number
    if(n1 > n2) return 1
    if(n1 < n2) return -1
    i++
  }

  // while(arr1.length && arr2.length){  // while arr1 and arr2 have elements
/*
  while(arr1.length && arr2.length){  // while arr1 and arr2 have elements
    const n1 = Number(arr1.shift())  // shift the first element of arr1 and convert it to a number
    const n2 = Number(arr2.shift()) // shift the first element of arr2 and convert it to a number
    if(n1 > n2) return 1
    if(n1 < n2) return -1
    //return 0
  }
*/

  return 0;
};
let version1 = '1.0.01' // 1.0.0
let version2 = '1';
console.log(compareVersion(version1, version2));

